<template>
  <view class="container">
    <NavBar :title="title" />
    <view class="search_page">
      <Search @searchList="searchList" />
      <!-- 数据列表 1 -->
      <view class="list">
        <view v-if="!total">
          <NoData />
        </view>
        <view v-else>
          <!-- 视频学习 -->
          <view class="list_arr" v-if="params.learningType == 1">
            <view
              class="video_item"
              v-for="(i, index) in listArr"
              :key="index"
              @click="clickVideo(i)"
            >
              <VideoItem :itemObj="i" />
            </view>
          </view>
          <!-- 文档资料 -->
          <view class="list_arr" v-if="params.learningType == 2">
            <view
              class="file_item"
              v-for="(i, index) in listArr"
              :key="index"
              @click="clickFileItem(i)"
            >
              <FileItem :itemObj="i" />
            </view>
          </view>
          <!-- 电子图书 -->
          <view class="list_arr" v-if="params.learningType == 3">
            <view
              class="e_books"
              v-for="(i, index) in listArr"
              :key="index"
              @click="clickFileItem(i)"
            >
              <EBooks :itemObj="i" />
            </view>
          </view>
          <!-- 专题学习 -->
          <view class="list_arr" v-if="params.learningType == 4">
            <view
              class="special_item"
              v-for="(i, index) in listArr"
              :key="index"
              @click="clickFileItem(i)"
            >
              <Special :itemObj="i" />
            </view>
          </view>
        </view>
      </view>
      <!-- 数据列表 2 -->
    </view>
  </view>
</template>

<script>
// 接口
import {
  apiUploadList, // 5 创建的教育学习列表
  // apiCollectionList, // 10 收藏列表
  apiWatchInfo, // 13 观看进度
  apiClickUpdateCount, // 修改每个学习类型未读文件数量
  // apiCommonEducSave, // 记录党员每个学习类型已读未读文件数量
  apiCommonIsRead, // 18 统计每个党员的已读文章状态
} from "../../net/educationalLearning.js";

import NavBar from "@/components/NavBar.vue";
import NoData from "@/components/NoData";

import StudyItem from "./common/study_item.vue";
import Search from "./common/search.vue";

import VideoItem from "./common/video_item.vue"; // 视频学习
import FileItem from "./common/file_item.vue"; // 文档资料
import EBooks from "./common/EBooks.vue"; // 电子图书
import Special from "./common/special.vue"; // 专题学习

export default {
  components: {
    NavBar,
    NoData,

    StudyItem,
    Search,
    VideoItem,
    FileItem,
    EBooks,
    Special,
  },
  data() {
    return {
      watchArr: [],
      title: "",
      total: 0,
      listArr: [],
      params: {
        learningType: 0,
        title: "",
        status: 1, //上传  2 下载
        userId: JSON.parse(uni.getStorageSync("loginUserInfo")).uid,
        pageNum: 1,
        pageSize: 10,
      },
    };
  },

  onLoad(options) {
    let pageInfo = JSON.parse(options.data);
    this.title = pageInfo.title;
    this.params.learningType = Number(pageInfo.index) + 1;
    // this.init();
  },
  onShow() {
    this.params.pageNum = 1;
    this.total = 0;
    this.listArr = [];
    this.init();
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.params.title = "";
    this.params.pageNum = 1;
    this.total = 0;
    this.listArr = [];
    this.getUploadList();
  },

  // 上拉加载
  onReachBottom() {
    if (this.params.pageNum * this.params.pageSize >= this.total) {
      uni.showToast({
        title: "数据全部加载",
        duration: 2000,
      });
    } else {
      // this.total = 0;
      this.params.pageNum++;
      this.getUploadList();
    }
  },

  methods: {
    uniShowToast(title, icon = "success", duration = 1500) {
      uni.showToast({
        title,
        icon,
        mask: true,
        duration,
      });
    },

    // ------------------------
    // 搜索
    searchList(e) {
      this.params.title = e;
      this.listArr = [];
      this.getUploadList();
    },

    updateClickUpdateCount(item) {
      console.log(item);
      let data = {
        userId: JSON.parse(uni.getStorageSync("loginUserInfo")).uid,
        djStudyId: item.djStudyId,
        type: item.learningType,
      };
      apiClickUpdateCount(data);
    },

    // getCommonEducSave(item) {
    // 	console.log(item);
    //   let permission = JSON.parse(
    //     uni.getStorageSync('loginUserInfo')
    //   ).permission;
    //   let data = {
    //     id: item.djStudyId,
    //     userId: JSON.parse(uni.getStorageSync('loginUserInfo')).uid,
    //   };
    //   if (permission.indexOf("common") > -1) {
    //     apiCommonEducSave(data);
    //   }
    // },

    // 点击跳转
    clickFileItem(item) {
      console.log(item);
      this.updateClickUpdateCount(item);
      // this.getCommonEducSave(item);
      let params = {
        ...item,
        flag: true,
      };
      if (item.learningType == 2) {
        params.Type = "文档资料";
      } else if (item.learningType == 3) {
        params.Type = "电子图书";
      } else if (item.learningType == 4) {
        params.Type = "专题学习";
      }
      uni.setStorage({
        key: "itemParams",
        data: JSON.stringify(params),
      });
      uni.navigateTo({
        url: `./historyInfo`,
      });
    },

    // 点击播放视频
    clickVideo(item) {
      console.log(item);
      this.updateClickUpdateCount(item);
      // this.getCommonEducSave(item);
      uni.navigateTo({
        url: `./videoPage?item=${JSON.stringify(item)}`,
      });
    },

    // 学习列表
    getUploadList() {
      apiUploadList(this.params)
        .then((res) => {
          if (res.code === 0) {
            this.total = res.total;
            console.log(res.data, "111");
            this.listArr = this.listArr.concat(res.data);
            let permission = JSON.parse(
              uni.getStorageSync("loginUserInfo")
            ).permission;
            for (let i = 0; i < this.listArr.length; i++) {
              this.listArr[i].permission = permission[permission.length - 1];
            }
            // 视频学习
            if (this.params.learningType == 1) {
              for (let i = 0; i < this.listArr.length; i++) {
                this.listArr[i].isChoose = false;
                this.listArr[i].permission = permission[permission.length - 1];
                // 视频链接
                if (typeof this.listArr[i].url == "string") {
                  this.listArr[i].url = JSON.parse(this.listArr[i].url).map(
                    (v) => {
                      return {
                        src: v,
                        collectId: null,
                      };
                    }
                  );
                }
              }
              // 观看进度
              let data = {
                userId: JSON.parse(uni.getStorageSync("loginUserInfo")).uid,
              };
              apiWatchInfo(data)
                .then((res) => {
                  if (res.code == 0 && res.data.length && this.listArr.length) {
                    for (let i = 0; i < this.listArr.length; i++) {
                      for (let j = 0; j < res.data.length; j++) {
                        if (
                          this.listArr[i].djStudyId == res.data[j].djStudyId
                        ) {
                          this.listArr[i].watchProcess = res.data[j].schedule;
                        }
                      }
                    }
                  } else {
                    this.watchArr = [];
                  }
                })
                .catch((err) => {
                  console.log(err);
                  this.watchArr = [];
                });
            }

            if (permission[permission.length - 1] == "common") {
              let data = {
                userId: JSON.parse(uni.getStorageSync("loginUserInfo")).uid,
                djStudyId: this.params.learningType,
                djDeptId: JSON.parse(uni.getStorageSync("loginUserInfo"))
                  .djDeptId,
              };
              apiCommonIsRead(data)
                .then((res) => {
                  if (res.code == 0 && res.data && this.listArr) {
                    let data = res.data;
                    for (let i = 0; i < data.length; i++) {
                      for (let j = 0; j < this.listArr.length; j++) {
                        if (data[i].dj_study_id == this.listArr[j].djStudyId) {
                          this.listArr[j].status = data[i].status;
                        }
                      }
                    }
                  }
                  console.log(this.listArr);
                })
                .catch();
            }
          } else {
            this.total = 0;
            this.listArr = [];
          }
          uni.stopPullDownRefresh();
        })
        .catch((err) => {
          console.log(err);
          this.total = 0;
          this.listArr = [];
          uni.stopPullDownRefresh();
        });
    },
    init() {
      this.getUploadList();
    },
  },
};
</script>

<style>
@import url("common/index.css");

page {
  background: #f7f7f7;
  height: 101% !important;
}

/* 数据列表 1 */
.list {
  margin: 0 20rpx;
}

.list_arr {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  margin-top: 28rpx;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
}

.video_item {
  position: relative;
  width: 100%;
}

.file_item {
  position: relative;
  width: 100%;
}

.special_item {
  position: relative;
}

/* 数据列表 2 */
</style>
